import collections
import queue
from copy import copy


class Graph(object):
    def __init__(self, value):
        self.value = value
        self.nodes = []
    
    def add_node(self, node):
        self.nodes.append(node)


class Solution:
    def DFS(self, graph_list, graph):
        for node in graph.nodes:
            if node in graph_list:
                continue
            else:
                graph_list.append(node)
                self.DFS(graph_list, node)
    
    def findCircleNum(self, M):
        graphs = []
        for i in range(len(M)):
            graph = Graph(i)
            graphs.append(graph)
        
        for i in range(len(M)):
            for j, value in enumerate(M[i]):
                if value == 1:
                    graphs[i].add_node(graphs[j])
        
        have_circle = []
        circle_num = 0
        for graph in graphs:
            if graph in have_circle:
                continue
            else:
                graph_list = []
                self.DFS(graph_list, graph)
                have_circle.extend(graph_list)
                circle_num += 1
        
        return circle_num


def main():
    sol = Solution()
    print(sol.findCircleNum(
        [[1, 1, 0],
         [1, 1, 0],
         [0, 0, 1]]))


if __name__ == "__main__":
    main()
